package com.ontheroad.system.web;

import com.alibaba.fastjson.JSON;
import com.ontheroad.core.annotation.OpLog;
import com.ontheroad.core.util.MD5Encoder;
import com.ontheroad.core.web.BaseController;
import com.ontheroad.map.Feature;
import com.ontheroad.map.GeoJson;
import com.ontheroad.map.Geometry;
import com.ontheroad.system.entity.SysUser;
import com.ontheroad.system.service.SysMenuService;
import com.ontheroad.system.service.SysUserService;
import com.ontheroad.user.model.UserModel;
import com.ontheroad.user.service.SystemUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Scope("session")
@RequestMapping(value = "/index")
@OpLog(logDesc = "系统操作员登陆")
public class IndexController extends BaseController {
    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SystemUserService userService;

    @RequestMapping(value = "tree")
    public void index(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
        SysUser user = (SysUser) session.getAttribute("user");
        String menuTree = sysMenuService.listUserTree(Long.valueOf(user.getUserId()), false);
        writeToPage(menuTree, response);
    }

    @RequestMapping(value = "password")
    public String password() {
        return "password";
    }

    @RequestMapping(value = "checkUserPwd")
    public void checkUserPwd(HttpServletRequest request, PrintWriter out) {
        SysUser sUser = (SysUser) request.getSession().getAttribute("user");
        String password = MD5Encoder.encode(request.getParameter("password"));
        String msg = "操作成功";
        boolean result = false;
        if (!password.equals(sUser.getUserPwd())) {
            result = true;
            msg = "原始密码不正确";
        }
        ajaxJsonResponse(out, result, msg);
    }

    @RequestMapping(value = "update")
    public String update(HttpServletRequest request) {
        String userPwd = request.getParameter("newPwd");
        SysUser sUser = (SysUser) request.getSession().getAttribute("user");
        sUser.setUserPwd(userPwd);
        sysUserService.updateUserPwd(sUser);
        return "success";
    }

    /**
     * 主页
     *
     * @param map
     * @return
     */
    @RequestMapping("index")
    public String index(Map map) {
        // 查询在线用户
        List<UserModel> users = userService.getOnlineUserList();
        map.put("geojson", JSON.toJSONString(getGeoJsonByUsers(users)));
        return "system/main";
    }

    private GeoJson getGeoJsonByUsers(List<UserModel> users) {
        GeoJson geoJson = new GeoJson();
        if (users != null && users.size() > 0) {
            for (UserModel user : users) {
                String lng = user.getLng();
                String lat = user.getLat();
                if (StringUtils.isEmpty(lng) || StringUtils.isEmpty(lat)) {
                    continue;
                }
                Feature feature = new Feature();
                Map<String, Object> map = new HashMap();
                map.put("id", user.getId());
                map.put("nick", user.getNick());
                map.put("pic", user.getPic());
                map.put("sex", user.getSex());
                feature.setProperties(map);

                try {
                    Geometry geometry = new Geometry();
                    Double[] coordinate = new Double[]{Double.parseDouble(lng), Double.parseDouble(lat)};
                    geometry.setCoordinates(coordinate);
                    feature.setGeometry(geometry);

                    geoJson.getFeatures().add(feature);
                } catch (Exception e) {
                    e.printStackTrace();
                }


            }
        }
        return geoJson;
    }
}
